import 'dart:async';
import 'package:huxiaodui/config.dart';
import 'package:huxiaodui/utils/global_controller.dart';
import 'package:huxiaodui/view/index/map_page/map_controller.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';
import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class MapPage extends StatefulWidget {
  const MapPage({super.key});

  @override
  _MapPageState createState() => _MapPageState();
}

class _MapPageState extends State<MapPage> {
  /// 是否显示微信地图
  bool isSatellite = false;

  /// 是否为当前位置
  bool isCurrLatLng = false;

  void onMapCreated(AMapController controller) {
    MapController().mapController = controller;
  }


  @override
  Widget build(BuildContext context) {
    GlobalController global = Provider.of<GlobalController>(context);
    MapController controller = Provider.of<MapController>(context);

    /* 高德地图 地图组件 */
    AMapWidget map = AMapWidget(
      initialCameraPosition: CameraPosition(target: controller.currLatLng ??  const LatLng(39.909187, 116.397451),zoom: 10),
      ///必须正确设置的合规隐私声明，否则SDK不会工作，会造成地图白屏等问题。
      privacyStatement: const AMapPrivacyStatement(
          hasContains: true, hasShow: true, hasAgree: true),
      apiKey: AMapApiKey(androidKey: AmapConfig.androidKey, iosKey: AmapConfig.iosKey),
      onMapCreated: onMapCreated,
      myLocationStyleOptions: MyLocationStyleOptions(false),
      mapType: isSatellite
          ? MapType.satellite
          : (Theme.of(context).brightness == Brightness.dark
              ? MapType.night
              : MapType.normal),
      // compassEnabled: true,
      markers: controller.markers,

      onCameraMoveEnd: (val) {
        controller.currZoom = val.zoom;
      },
      onLocationChanged: (val){
        controller.currLatLng = val.latLng;
        controller.refresh();
        print("地图定位：${val.toString()}");
      },
    );

    /* 开关卫星地图 按钮 */
    Widget satelliteButton = Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.0),
        color: Theme.of(context).appBarTheme.backgroundColor,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(90), // 阴影颜色
            offset: const Offset(2.0, 2.0), // 阴影偏移量
            blurRadius: 5.0, // 模糊半径
          ),
        ],
      ),
      child: Transform.scale(
        scale: 2,
        child: IconButton(
          onPressed: () => setState(() => isSatellite = !isSatellite),
          icon: const Icon(Icons.account_balance),
          color: isSatellite
              ? Colors.tealAccent
              : Theme.of(context).appBarTheme.foregroundColor,
        ),
      ),
    );

    /* 跳到当前位置 按钮 */
    Widget currLatLngButton = Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.0),
        color: Theme.of(context).appBarTheme.backgroundColor,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(90), // 阴影颜色
            offset: const Offset(2.0, 2.0), // 阴影偏移量
            blurRadius: 5.0, // 模糊半径
          ),
        ],
      ),
      child: Transform.scale(
        scale: 2,
        child: IconButton(
          onPressed: () async {
            // await global.getPosition();
            setState(() {
              isCurrLatLng = true;
              if (controller.currLatLng != null) {
                MapController().moveMap(latLng:controller.currLatLng!);
              }
            });
            Future.delayed(const Duration(milliseconds: 150), () {
              setState(() {

                isCurrLatLng = false;
              });
            });
          },
          icon: const Icon(Icons.location_pin),
          color: isCurrLatLng
              ? Colors.tealAccent
              : Theme.of(context).appBarTheme.foregroundColor,
        ),
      ),
    );

    return Stack(
      children: [
        Positioned(child: map),
        Positioned(bottom: 50, right: 20, child: satelliteButton),
        Positioned(bottom: 120, right: 20, child: currLatLngButton),
        AnimatedSwitcher(
          duration: const Duration(milliseconds: 150),
          transitionBuilder: (Widget child, Animation<double> animation) {
            return ScaleTransition(scale: animation, child: child);
          },
          child: Container(
            alignment: Alignment.topCenter,
            child: controller.showMarkerInfo,),
        ),
      ],
    );
  }
}
